package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.openCardTask.EMPListData;
import com.burgeon.backend.SHOPBACKEND.entity.openCardTask.StoreListData;
import com.burgeon.backend.SHOPBACKEND.entity.openCardTask.VipListViewData;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendEmployeeMapper;
import com.burgeon.backend.SHOPBACKEND.rest.openCardTask.*;
import com.burgeon.backend.SHOPBACKEND.rest.openCardTask.util.FindStoreName;
import com.burgeon.backend.SHOPBACKEND.rest.openCardTask.util.FirstTaskView;
import com.burgeon.backend.SHOPBACKEND.service.BackendOpenCardTaskService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@Service
public class BackendOpenCardTaskServiceImpl implements BackendOpenCardTaskService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BackendEmployeeMapper backendEmployeeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public HashMap topData(String yearmonth, String account) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        if (null == yearmonth) {
            yearmonth = sdf.format(new Date());
        }

        Info info = GetInfo(account);
        FirstTaskView firstTaskView = new FirstTaskView(info.getUrl(), info.getAccount(), info.getPassword());
        TopDataEmp topDataEmp = new TopDataEmp(info.getUrl(), info.getAccount(), info.getPassword());
        TopDataStore topDataStore = new TopDataStore(info.getUrl(), info.getAccount(), info.getPassword());

        HashMap data = new HashMap();
        try {
            firstTaskView.getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {

            data.put("emp", topDataEmp.getData(yearmonth));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            data.put("store", topDataStore.getData(yearmonth));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    @Override
    public HashMap vipList(Integer start, String account) {
        Info info = GetInfo(account);
        VipListView vipListView = new VipListView(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap data = new HashMap();
        try {
            ArrayList<VipListViewData> vipListViewData = vipListView.getData(start);

            for (VipListViewData vip : vipListViewData) {
                vip.setEmpImg(backendEmployeeMapper.findData(vip.getEmpId(), account));
                User user = userMapper.FindUserDataByPhone(vip.getVipPhone(), account);
                if (null != user) {
                    vip.setVipImg(user.getImg());
                }

            }
            data.put("list", vipListViewData);
            data.put("count", vipListViewData.get(0).getCount());
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @param start
     * @param field   A 卡号 B 开卡员工 C开卡门店 D开卡时间
     * @param value
     * @param account
     * @return
     */
    @Override
    public HashMap vipListFind(Integer start, String field, String value, String account) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("A", "CARDNO");
        map.put("B", "HR_EMPLOYEE_ID;NAME");
        map.put("C", "C_STORE_ID;NAME");
        map.put("D", "OPENCARDDATE");

        Info info = GetInfo(account);
        FindVipListView findVipListView = new FindVipListView(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap data = new HashMap();
        try {
            ArrayList<VipListViewData> vipListViewData = findVipListView.getData(map.get(field), value, start);
            for (VipListViewData vip : vipListViewData) {
                vip.setEmpImg(backendEmployeeMapper.findData(vip.getEmpId(), account));
                User user = userMapper.FindUserDataByPhone(vip.getVipPhone(), account);
                if (null != user) {
                    vip.setVipImg(user.getImg());
                }

            }
            data.put("list", vipListViewData);
            data.put("count", vipListViewData.get(0).getCount());
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param field   A 店名 B销售区域 C品牌 D无条件
     * @param value
     * @param start
     * @param account
     * @return
     */
    @Override
    public HashMap storeList(String field, String value, Integer start, String time, String account) {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        if (null == time){
           time = sdf.format(new Date());
       }
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("A", "NAME");
        map.put("B", "C_AREA_ID;NAME");
        map.put("C", "M_DIM1_ID;ATTRIBNAME");
        map.put("D", null);
        Info info = GetInfo(account);
        FindStoreListView findStoreListView = new FindStoreListView(info.getUrl(), info.getAccount(), info.getPassword());
        FindSTOREODMARKListView findSTOREODMARKListView = new FindSTOREODMARKListView(info.getUrl(), info.getAccount(), info.getPassword());
        try {

            ArrayList<StoreListData> data = findStoreListView.getData(map.get(field), value, start);
            findSTOREODMARKListView.getData(data, time);

            HashMap row = new HashMap();
            row.put("store", data);
            if (data.size() == 0) {
                row.put("count", 0);
            } else {
                row.put("count", data.get(0).getCount());
            }

            return row;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public ArrayList<EMPListData> empList(String id, String account) {
        Info info = GetInfo(account);
        FindEmpListView findEmpListView = new FindEmpListView(info.getUrl(), info.getAccount(), info.getPassword());

        try {
            ArrayList<EMPListData> data = findEmpListView.getData(id);
            for (EMPListData emp:data){
                emp.setImg(backendEmployeeMapper.findData(emp.getEmpId(), account));
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void setTask(String[] ids, String value,String time, String account) throws ErrorDataException {
        Info info = GetInfo(account);

        ModTaskTarget modTaskTarget = new ModTaskTarget(info.getUrl(), info.getAccount(), info.getPassword());
        AddTaskData addTaskData = new AddTaskData(info.getUrl(), info.getAccount(), info.getPassword());
        FindStoreName findStoreName = new FindStoreName(info.getUrl(), info.getAccount(), info.getPassword());
        FindSTOREODMARKByModTask findSTOREODMARKByModTask = new FindSTOREODMARKByModTask(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            ArrayList<HashMap<String, String>> data = findSTOREODMARKByModTask.getData(ids, time);
            for (HashMap<String, String> bean : data){
                if ("Y".equals(bean.get("exist"))){//修改
                    modTaskTarget.getData(bean.get("mid"),value);
                }else {//新增
                    String store = findStoreName.getData(bean.get("sid"));
                    addTaskData.getData(store,time,value);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
    }

    private Info GetInfo(String account) {
        return accountMapper.GetAccData(account);
    }


}
